﻿#include <tchar.h>
#include <time.h>
#include <windows.h>
#include <list>
using namespace std;
#include "../../../../NetEngine_Windows/NetEngine_SourceCode/XyRyNet_CommHdr.h"
#include "../../../../NetEngine_Windows/NetEngine_SourceCode/XyRyNet_ProtocolHdr.h"
#include "../../../../NetEngine_Windows/NetEngine_SourceCode/NetEngine_StreamMedia/StreamMedia_HLSProtocol/HLSProtocol_Define.h"
#include "../../../../NetEngine_Windows/NetEngine_SourceCode/NetEngine_StreamMedia/StreamMedia_HLSProtocol/HLSProtocol_Error.h"

#pragma comment(lib,"../../../../NetEngine_Windows/NetEngine_SourceCode/Debug/StreamMedia_HLSProtocol.lib")
#pragma comment(lib,"Ws2_32.lib")

XNETHANDLE xhRoot;
XNETHANDLE xhLow;
XNETHANDLE xhNormal;
XNETHANDLE xhHigh;

void __stdcall HLSProtocol_CBNotify(LPCTSTR lpszFileName, double dlTime, int nIndex, LPVOID lParam)
{
	printf("File:%s,Index:%d,Time:%lf\n", lpszFileName, nIndex, dlTime);
	HLSProtocol_M3u8File_AddFile(xhRoot, xhNormal, lpszFileName, dlTime, FALSE);
}

int main()
{
	XNETHANDLE xhToken = 0;

	LPCTSTR lpszVideoFile = _T("E:\\netengineapp\\NetEngine_WINApps\\Debug\\480p.264");
	LPCTSTR lpszAudioFile = _T("E:\\netengineapp\\NetEngine_WINApps\\Debug\\test.aac");
	LPCTSTR lpszDstFile = _T("E:\\netengineapp\\NetEngine_WINApps\\Debug\\live\\normal\\480p-");

	LPCTSTR lpszRootFile = _T("E:\\netengineapp\\NetEngine_WINApps\\Debug\\live\\live.m3u8");
	LPCTSTR lpszLowFile = _T("E:\\netengineapp\\NetEngine_WINApps\\Debug\\live\\low\\live.m3u8");
	LPCTSTR lpszNormalFile = _T("E:\\netengineapp\\NetEngine_WINApps\\Debug\\live\\normal\\live.m3u8");
	LPCTSTR lpszHighFile = _T("E:\\netengineapp\\NetEngine_WINApps\\Debug\\live\\high\\live.m3u8");

	if (!HLSProtocol_M3u8File_Create(&xhRoot, lpszRootFile))
	{
		printf("HLSProtocol_M3u8File_Create:%lX\n", HLSProtocol_GetLastError());
		return -1;
	}

	if (!HLSProtocol_M3u8File_AddStream(xhRoot, &xhLow, lpszLowFile, "www.xyry.org/live/low/live.m3u8", 150000))
	{
		printf("HLSProtocol_M3u8File_AddStream:%lX\n", HLSProtocol_GetLastError());
		return -1;
	}
	if (!HLSProtocol_M3u8File_AddStream(xhRoot, &xhNormal, lpszNormalFile, "www.xyry.org/live/normal/live.m3u8", 500000))
	{
		printf("HLSProtocol_M3u8File_AddStream:%lX\n", HLSProtocol_GetLastError());
		return -1;
	}
	if (!HLSProtocol_M3u8File_AddStream(xhRoot, &xhHigh, lpszHighFile, "www.xyry.org/live/high/live.m3u8", 2000000))
	{
		printf("HLSProtocol_M3u8File_AddStream:%lX\n", HLSProtocol_GetLastError());
		return -1;
	}

	if (!HLSProtocol_Section_Init(&xhToken, 12, HLSProtocol_CBNotify))
	{
		printf("HLSProtocol_Section_Init:%lX\n", HLSProtocol_GetLastError());
		return -1;
	}
	if (!HLSProtocol_Section_Input(xhToken, lpszVideoFile))
	{
		printf("HLSProtocol_Section_Input:%lX\n", HLSProtocol_GetLastError());
		return -1;
	}
	if (!HLSProtocol_Section_Output(xhToken, lpszDstFile))
	{
		printf("HLSProtocol_Section_Output:%lX\n", HLSProtocol_GetLastError());
		return -1;
	}
	if (!HLSProtocol_Section_Start(xhToken))
	{
		printf("HLSProtocol_Section_Start:%lX\n", HLSProtocol_GetLastError());
		return -1;
	}

	BOOL bRun = TRUE;
	while (bRun)
	{
		if (!HLSProtocol_Section_GetStatus(xhToken, &bRun))
		{
			break;
		}
	}
	HLSProtocol_Section_Close(xhToken);
	HLSProtocol_M3u8File_AddFile(xhRoot, xhNormal);
	//HLSProtocol_M3u8File_Delete(xhRoot, xhNormal);
	//HLSProtocol_M3u8File_Delete(xhRoot);
	printf("wandan\n");
	return 0;
}